package com.example.testng.utils;

import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.ITestContext;
import io.qameta.allure.Allure;

/**
 *  生成测试报告
 */
public class ReportGenerator implements ITestListener {

    @Override
    public void onTestStart(ITestResult result) {
        System.out.println("开始执行测试: " + result.getName());
    }

    @Override
    public void onTestSuccess(ITestResult result) {
        System.out.println("测试通过: " + result.getName());
        Allure.addAttachment("测试结果", "通过");
    }

    @Override
    public void onTestFailure(ITestResult result) {
        System.out.println("测试失败: " + result.getName());
        Allure.addAttachment("测试结果", "失败");
        Allure.addAttachment("失败原因", result.getThrowable().getMessage());
    }

    @Override
    public void onTestSkipped(ITestResult result) {
        System.out.println("测试跳过: " + result.getName());
        Allure.addAttachment("测试结果", "跳过");
    }

    @Override
    public void onFinish(ITestContext context) {
        System.out.println("测试套件执行完成");
        System.out.println("总用例数: " + context.getAllTestMethods().length);
        System.out.println("通过用例数: " + context.getPassedTests().size());
        System.out.println("失败用例数: " + context.getFailedTests().size());
        System.out.println("跳过用例数: " + context.getSkippedTests().size());
        System.out.println("开始时间: " + context.getStartDate());
        System.out.println("结束时间: " + context.getEndDate());
        System.out.println("执行时长: " +
                (context.getEndDate().getTime() - context.getStartDate().getTime()) / 1000 + "秒");
        System.out.println("----------------------------------------");
    }
}